package top.jacktgq.web.servlet;

import top.jacktgq.dao.StudentDao;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;

/**
 * @Author CandyWall
 * @Date 2021/6/20--19:54
 * @Description 读取二进制中的图片
 */
@WebServlet(name = "GetPhotoServlet", value = "/getPhoto")
public class GetPhotoServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        StudentDao studentDao = new StudentDao();

        try {
            byte[] photo_bytes = studentDao.getPhoto(request.getParameter("stu_id"));
            if (photo_bytes != null) {
                response.getOutputStream().write(photo_bytes);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}
